//
//  MCUrlProcess.m
//  MClient
//
//  Created by  史正烨 on 09-11-2.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MCUrlProcess.h"
#import "MServerDAO.h"
#import "MCFunction.h"
#import "MCErrorManagement.h"
#import "ListController.h"
#import "AListController.h"
#import	"ViewFormController.h"
#import "AViewFormController.h"
#import "EditFormController.h"
#import "QueryController.h"
#import "TreeViewController.h"
#import "ReportViewController.h"
#import "MCWebViewController.h"
#import "CustomFormController.h"
#import "CustomListController.h"
#import "CustomAListController.h"
#import "SettingController.h"
#import "MenuController.h"
#import "ShrinkTreeViewController.h"
#import "ListSortController.h"
#import "SelectFieldViewController.h"

typedef enum eActionTypeId {
	ActionTypeId_Forward_list,
	ActionTypeId_Forward_Edit,
	ActionTypeId_Forward_View,
	ActionTypeId_Save,
	ActionTypeId_Delete,
	ActionTypeId_Biz_Service,
	ActionTypeId_Calc,
	ActionTypeId_Forward_Tree,
	ActionTypeId_Report_Query,
	ActionTypeId_Report,
	ActionTypeId_Empty_url,
	ActionTypeId_Forward_Custom_Form,
	ActionTypeId_Forward_Custom_List,
	ActionTypeId_Html,
	ActionTypeId_Apps,
	ActionTypeId_MenuOfApp,
	ActionTypeId_SubMenu
} ActionTypeId;

//@interface MCUrlProcess (Private)
static ActionTypeId  actionTypeId;
//@end

static MCUrlProcess *mcUrlProcessInstance = nil;

@implementation MCUrlProcess
@synthesize url;
@synthesize confirmMessage;
@synthesize parameters;
@synthesize jump2NaviController;
@synthesize menuTitle;
#pragma mark ---- singleton object methods ----

// See "Creating a Singleton Instance" in the Cocoa Fundamentals Guide for more info

+ (MCUrlProcess *)sharedInstanceWithUrl:(NSDictionary *)aUrl 
							 parameters:(NSMutableDictionary *)aParameters 
						 confirmMessage:(NSString *)aConfirmMessage
						 naviController:(UINavigationController*)naviController {
	@synchronized(self) {
		if (mcUrlProcessInstance == nil) {
			mcUrlProcessInstance = [[self alloc] init]; // assignment not done here
			mcUrlProcessInstance = [[[[MCUserDefaults instance] class:@"MCUrlProcess"] alloc] init];
			
		}
		mcUrlProcessInstance.url = aUrl;
		mcUrlProcessInstance.confirmMessage = aConfirmMessage;
		mcUrlProcessInstance.parameters = aParameters;
		mcUrlProcessInstance.jump2NaviController = naviController;
	}
	return mcUrlProcessInstance;
}

- (id)retain {
	return self;
}

- (unsigned)retainCount {
	return UINT_MAX;  // denotes an object that cannot be released
}

- (void)release {
	//do nothing
}

- (id)autorelease {
	return self;
}
#pragma mark ---- 类基本方法 ----

- (id)initWithUrl:(NSMutableDictionary *)aUrl parameters:(NSMutableDictionary *)aParameters confirmMessage:(NSString *)aConfirmMessage {
	self = [super init];
	self.url = aUrl;
	self.confirmMessage = aConfirmMessage;
	self.parameters = aParameters;
	return self;
}//貌似没用到？

- (void)dealloc {
	[url release];
	[parameters release];
	[confirmMessage release];
	[menuTitle release];
	[super dealloc];
}

- (void)doUrlProcess {//huotianjun MCUrlProcess是负责页面跳转的处理器。MServerJsonDAO是负责包装指令的，指令完成由它的connect机制得到结果，将结果给MCUrlPorcess（页面跳转的情况，包括数据库增删改）或者各种ViewController。
	if (self.confirmMessage == nil) {
		@try {
			[self afterConfirm];
		}
		@catch (NSException * e) {
			if ([[e name] isEqualToString:@"isNotSupportURLException"]) {
				[[MCErrorManagement instance] doError:@"99" returnMessage:@"在手机应用不支持此项操作!"];
			}else {
				[[MCErrorManagement instance] doError:@"99" 
										returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
			}
		}
		return;
	}
	
	UIAlertView *alert= [[UIAlertView alloc] 
						 initWithTitle:confirmMessage 
						 message:nil
						 delegate:self 
						 cancelButtonTitle:@"取消" 
						 otherButtonTitles:@"确定",nil];
	[alert show];
	[alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 1) {
		@try {
			[self afterConfirm];
		}
		@catch (NSException * e) {
			if ([[e name] isEqualToString:@"isNotSupportURLException"]) {
				[[MCErrorManagement instance] doError:@"99" returnMessage:@"在手机应用不支持此项操作!"];
			}else {
				[[MCErrorManagement instance] doError:@"99" 
										returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
			}
		}
	}
}

- (void)afterConfirm {
	NSString *actionType = [url objectForKey:@"actionType"];
	
	NSMutableDictionary *otherParams = [NSMutableDictionary dictionary];
	if ([parameters objectForKey:@"buttonId"]) {
		[otherParams setObject:[parameters objectForKey:@"buttonId"] forKey:@"authValue"];
	} else if ([parameters objectForKey:@"authValue"]) {
		[otherParams setObject:[parameters objectForKey:@"authValue"] forKey:@"authValue"];
	}
    
	if ([parameters objectForKey:@"buttonType"]) {
		[otherParams setObject:[parameters objectForKey:@"buttonType"] forKey:@"authKey"];
	} else if ([parameters objectForKey:@"authKey"]) {
		[otherParams setObject:[parameters objectForKey:@"authKey"] forKey:@"authKey"];
	}
	
	if ([actionType isEqualToString:@"forward_list"]) {
		actionTypeId = ActionTypeId_Forward_list;
		[[MServerDAO sharedInstance]getListConfigAsync:[url objectForKey:@"dto"]
											layoutName:[url objectForKey:@"lytn"]
										   otherParams:otherParams
											  delegate:self];
	}
	else if ([actionType isEqualToString:@"forward_edit"]) {
		actionTypeId = ActionTypeId_Forward_Edit;
		[[MServerDAO sharedInstance] getFormConfigAsync:[url objectForKey:@"dto"]
											 layoutName:[url objectForKey:@"lytn"]
											   editable:@"1" 
											otherParams:otherParams
											   delegate:self];
	}
	else if ([actionType isEqualToString:@"forward_view"]) {
		actionTypeId = ActionTypeId_Forward_View;	
		[[MServerDAO sharedInstance] getFormConfigAsync:[url objectForKey:@"dto"]
											 layoutName:[url objectForKey:@"lytn"]
											   editable:@"0" 
											otherParams:otherParams
											   delegate:self];
	}
	else if ([actionType isEqualToString:@"save"]) {
		NSString *dataObjectName = [url objectForKey:@"dto"];
		if (dataObjectName == nil || [dataObjectName length] == 0) 
			dataObjectName = [parameters objectForKey:@"dataObjectName"];
		
		actionTypeId = ActionTypeId_Save;
		[[MServerDAO sharedInstance] saveFormDataAsync:[parameters objectForKey:@"buttonId"]
											buttonType:[parameters objectForKey:@"buttonType"]
										dataObjectName:dataObjectName
											  formData:[parameters objectForKey:@"formData"] 
										   otherParams:otherParams
											  delegate:self];
	}
	else if ([actionType isEqualToString:@"delete"]) {
		NSString *dataObjectName = [url objectForKey:@"dto"];
		if (dataObjectName == nil || [dataObjectName length] == 0) 
			dataObjectName = [parameters objectForKey:@"dataObjectName"];
		NSString *buttonId = [parameters objectForKey:@"buttonId"];
		NSString *buttonType = [parameters objectForKey:@"buttonType"];
		
		NSMutableArray *keys = [NSMutableArray array];
		if ([parameters objectForKey:@"recordArray"]) {
			NSMutableArray *recordArray = [parameters objectForKey:@"recordArray"];
			for (int i = 0; i < [recordArray count]; i++) {
				NSMutableDictionary *record = [recordArray objectAtIndex:i];
				[keys addObject:[record objectForKey:@"key"] == nil?@"":[record objectForKey:@"key"]];
			}
		}else {
			NSString *key = [url objectForKey:@"pkey"];
			if (key == nil || [key length] == 0) {
				key = [parameters objectForKey:@"primaryKey"];
			}
			if (key != nil || [key length] > 0) {
				[keys addObject:key];
			}
		}
		
		actionTypeId = ActionTypeId_Delete;
		[[MServerDAO sharedInstance] deleteDataAsync:buttonId 
										  buttonType:buttonType
									  dataObjectName:dataObjectName
												keys:keys
										 otherParams:otherParams
											delegate:self];
	}
	
	else if ([actionType isEqualToString:@"biz_service"]) {
		NSString *buttonId = [parameters objectForKey:@"buttonId"] == nil?@"":[parameters objectForKey:@"buttonId"];
		NSString *buttonType = [parameters objectForKey:@"buttonType"] == nil?@"":[parameters objectForKey:@"buttonType"];
		NSString *dataObjectName = [url objectForKey:@"dto"];
		NSString *seviceDefId = [url objectForKey:@"sid"] == nil?@"":[url objectForKey:@"sid"];
		if (dataObjectName == nil || [dataObjectName length] == 0) 
			dataObjectName = [parameters objectForKey:@"dataObjectName"] == nil?@"":[parameters objectForKey:@"dataObjectName"];
		
		NSMutableDictionary *formData = [parameters objectForKey:@"formData"];
		
		NSMutableArray *recordlist = [NSMutableArray array];
		if ([parameters objectForKey:@"recordArray"]) {
			NSMutableArray *recordArray = [parameters objectForKey:@"recordArray"];
			for (int i = 0; i < [recordArray count]; i++) {
				NSMutableDictionary *record = [recordArray objectAtIndex:i];
				[recordlist addObject:record == nil?[NSMutableDictionary dictionary]:record];
			}
		}else {
			if (formData != nil) {
				[recordlist addObject:formData];
			}
		}
        
		actionTypeId = ActionTypeId_Biz_Service;
		[[MServerDAO sharedInstance] doServiceByRecordAsync:buttonId
												 buttonType:buttonType
											 dataObjectName:dataObjectName
													records:recordlist
											   serviceDefId:seviceDefId
												otherParams:otherParams
												   delegate:self];
	}
	else if ([actionType isEqualToString:@"calc"]) {
		NSString *lookupBack = [parameters objectForKey:@"lookupBack"] == nil?@"":[parameters objectForKey:@"lookupBack"];
		NSString *dataObjectName = [parameters objectForKey:@"dataObjectName"] == nil?@"":[parameters objectForKey:@"dataObjectName"];
		NSString *seviceDefId = [parameters objectForKey:@"sid"] == nil?@"":[parameters objectForKey:@"sid"];
		NSMutableDictionary *formData = [parameters objectForKey:@"formData"];
		NSMutableArray *recordlist = [NSMutableArray array];
		if (formData != nil) {
			[recordlist addObject:formData];
		}
		
		actionTypeId = ActionTypeId_Calc;
		[[MServerDAO sharedInstance] calcAsync:lookupBack
								dataObjectName:dataObjectName
									   records:recordlist
								  serviceDefId:seviceDefId
									  delegate:self];
	}	
	else if ([actionType isEqualToString:@"forward_tree"]){
		NSString *treeId = [url objectForKey:@"id"];
		NSMutableArray *queryFilter = [NSMutableArray array];
		
		actionTypeId = ActionTypeId_Forward_Tree;
		[[MServerDAO sharedInstance] getTreeAsync:treeId
									  queryFilter:queryFilter
										 delegate:self];
	}
	else if ([actionType isEqualToString:@"report_query"]) {
		NSString *reportDefId = [url objectForKey:@"rptid"];
		
		actionTypeId = ActionTypeId_Report_Query;
		[[MServerDAO sharedInstance] getReportConfigAsync:reportDefId
											  otherParams:otherParams
												 delegate:self];
	}
	else if ([actionType isEqualToString:@"report"]) {
		actionTypeId = ActionTypeId_Report;
		NSString *reportDefId = [url objectForKey:@"rptid"];
		NSMutableArray *initialValues = [url objectForKey:@"qflt"];
		ReportViewController *reportViewController = [[ReportViewController alloc]init];
		reportViewController.reportDefId = reportDefId;
		reportViewController.initialValues = initialValues;
		[jump2NaviController pushViewController:reportViewController animated:YES];
		[reportViewController getDataFromServer];
		[reportViewController release];
	}
	else if ([actionType isEqualToString:@"empty_url"]) {
		actionTypeId = ActionTypeId_Empty_url;
	}
	
	else if ([actionType isEqualToString:@"forward_custom_form"]) {
		NSString *layoutName = [url objectForKey:@"id"];
		NSMutableArray *queryFilter = [url objectForKey:@"qflt"] == nil?[NSMutableArray array]:[url objectForKey:@"qflt"];
		NSString *key = [url objectForKey:@"pkey"] == nil?@"":[url objectForKey:@"pkey"];
		
		actionTypeId = ActionTypeId_Forward_Custom_Form;
		[[MServerDAO sharedInstance] getCustomFormAsync:key
											 layoutName:layoutName
											queryFilter:queryFilter
											otherParams:otherParams
											   delegate:self];	
	}
	else if ([actionType isEqualToString:@"forward_custom_list"]) {
		NSString *layoutName = [url objectForKey:@"id"];
		NSMutableArray *queryFilter = [url objectForKey:@"qflt"] == nil?[NSMutableArray array]:[url objectForKey:@"qflt"];
		
		actionTypeId = ActionTypeId_Forward_Custom_List;
		if (NO) {
			CustomListController *customListController = [[CustomListController alloc] init];
			customListController.dataObjectName = @"";
			customListController.layoutName = layoutName;
			customListController.queryFilter = queryFilter;
			customListController.page = 1;
			customListController.otherParams = otherParams;
			[jump2NaviController pushViewController:customListController animated:YES];
			[customListController getDataFromServer];
			
			[customListController release];
		}
		else {
			CustomAListController *customAListController = [[CustomAListController alloc] initWithStyle:UITableViewStylePlain];
			customAListController.dataObjectName = @"";
			customAListController.layoutName = layoutName;
			customAListController.queryFilter = queryFilter;
			customAListController.page = 1;
			customAListController.otherParams = otherParams;
			[jump2NaviController pushViewController:customAListController animated:YES];
			[customAListController getDataFromServer];
			
			[customAListController release];
		}
	}
	else if([actionType isEqualToString:@"html"]){
		MCWebViewController *htmViewController = [[MCWebViewController alloc] init];
		actionTypeId = ActionTypeId_Html;
		MCUserDefaults *defaults = [MCUserDefaults instance];
		NSString *serverUrl = [defaults.current_ServerUrl stringByAppendingString:@"/beapp/"];
		htmViewController.strLinkEnableFlag = @"DISABLE";
		[jump2NaviController pushViewController:htmViewController animated:YES];
		if ([[url objectForKey:@"url"] hasPrefix:@"http"]) {
			[htmViewController loadHTMLfromURL:[url objectForKey:@"url"]];
		}else {
			[htmViewController loadHTMLfromURL:[serverUrl stringByAppendingString:[url objectForKey:@"url"]]];
		}
		[htmViewController release];
	}
	else if([actionType isEqualToString:@"forward_apps"]){
		actionTypeId = ActionTypeId_Apps;
		[[MServerDAO sharedInstance] getAppsAsync:self];
	}
	else if([actionType isEqualToString:@"forward_menuofapp"]){
		NSString *menuId = [url objectForKey:@"id"];
		if (menuTitle) {
			[menuTitle release];
			menuTitle = nil;
		}
		menuTitle = [url objectForKey:@"lbl"];
		actionTypeId = ActionTypeId_MenuOfApp;
		[[MServerDAO sharedInstance] getMenuOfAppAsync:menuId
										   otherParams:otherParams
											  delegate:self];
	}
	else if([actionType isEqualToString:@"forward_submenu"]){
		NSString *menuId = [url objectForKey:@"id"];
		if (menuTitle) {
			[menuTitle release];
			menuTitle = nil;
		}
		menuTitle = [url objectForKey:@"lbl"];
		actionTypeId = ActionTypeId_SubMenu;
		[[MServerDAO sharedInstance] getSubMenuAsync:menuId
										 otherParams:otherParams
											delegate:self];
	}
	else {
		@throw [NSException exceptionWithName:@"isNotSupportURLException" reason:nil userInfo:nil];
	}		
}



#pragma mark ---- 异步回调方法 ----
- (void)getListConfigSuccess: (NSMutableDictionary *)returnValue {
	@try {
		NSString *dataObjectName = [url objectForKey:@"dto"];
		NSString *layoutName = [url objectForKey:@"lytn"];
		NSMutableArray *queryFilter = nil;//[[NSMutableArray alloc]init];
		if ([url objectForKey:@"qflt"]) {
			queryFilter = [url objectForKey:@"qflt"];
		} else {
			queryFilter = [NSMutableArray array];
		}
        
		
		NSMutableDictionary *listLayout = returnValue ;
		bool queryFirst = NO;
		if ([listLayout objectForKey:@"qft"]) {
			queryFirst = [[listLayout objectForKey:@"qft"]boolValue];
		}
		if (queryFirst) {
			QueryController *queryController = [[QueryController alloc] initWithStyle:UITableViewStylePlain];
			queryController.dataObjectName = dataObjectName;
			queryController.layoutName = layoutName;
			queryController.listLayout = listLayout;
			queryController.queryFilter = queryFilter;
			queryController.queryFirst = queryFirst;
			[jump2NaviController pushViewController:queryController animated:YES];
			[queryController release];					
			return;
		}
		//MCUserDefaults *defaults = [MCUserDefaults instance];
		
		if ([[listLayout objectForKey:@"style"] isEqualToString:@"3"]) {
			AListController *advanceListController = [[AListController alloc] initWithStyle:UITableViewStylePlain];
			advanceListController.dataObjectName = dataObjectName;
			advanceListController.layoutName = layoutName;
			advanceListController.listLayout = listLayout;
			advanceListController.queryFilter = queryFilter;
			advanceListController.page = 1;
			[jump2NaviController pushViewController:advanceListController animated:YES];
			[advanceListController getDataFromServer];
			
			[advanceListController release];
		}
		else {
			bool isSortList = [[[listLayout objectForKey:@"csty"] objectForKey:@"sortlist"] intValue];
			if (isSortList) {
				ListSortController *listController = [[ListSortController alloc] initWithStyle:UITableViewStylePlain];
				listController.dataObjectName = dataObjectName;
				listController.layoutName = layoutName;
				listController.listLayout = listLayout;
				listController.queryFilter = queryFilter;
				listController.page = 1;
				[jump2NaviController pushViewController:listController animated:YES];
				[listController getDataFromServer];
				
				[listController release];
			}else {
				ListController *listController = [[ListController alloc] init];
				listController.dataObjectName = dataObjectName;
				listController.layoutName = layoutName;
				listController.listLayout = listLayout;
				listController.queryFilter = queryFilter;
				listController.page = 1;
				[jump2NaviController pushViewController:listController animated:YES];
				[listController getDataFromServer];
				
				[listController release];
			}
		}
	} 
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void) getEditFormConfigSuccess: (NSMutableDictionary *)returnValue {
	@try {		
		NSString *dataObjectName = [url objectForKey:@"dto"];
		NSString *layoutName = [url objectForKey:@"lytn"];
		NSMutableArray *initialValues = nil;
		if ([url objectForKey:@"qflt"]) {
			initialValues = [url objectForKey:@"qflt"];
		} else {
			initialValues = [NSMutableArray array];
		}
        
		NSString *keyValue = [url objectForKey:@"pkey"];
		if (keyValue == nil) {
			keyValue = @"";
		}
		
		EditFormController *editFormController = [[[[MCUserDefaults instance] class:@"EditFormController"] alloc] initWithStyle:UITableViewStylePlain];
		
		editFormController.dataObjectName = dataObjectName;
		editFormController.layoutName = layoutName;
		editFormController.keyValue = keyValue;
		editFormController.initialValues = initialValues;
		editFormController.formLayout = returnValue;
		[jump2NaviController pushViewController:editFormController animated:YES];
		[editFormController getDataFromServer];
		[editFormController release];
	} 
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void) getViewFormConfigSuccess: (NSMutableDictionary *)returnValue {
	@try {
		
		NSString *dataObjectName = [url objectForKey:@"dto"];
		NSString *layoutName = [url objectForKey:@"lytn"];
		
		NSMutableArray *initialValues = nil;
		if ([url objectForKey:@"qflt"]) {
			initialValues = [url objectForKey:@"qflt"];
		} else {
			initialValues = [NSMutableArray array];
		}
        
		NSString *keyValue = [url objectForKey:@"pkey"];
		if (keyValue == nil) {
			keyValue = @"";
		}
		
		if (NO) {
			AViewFormController *viewFormController = [[AViewFormController alloc]initWithStyle:UITableViewStylePlain];
			viewFormController.dataObjectName = dataObjectName;
			viewFormController.layoutName = layoutName;
			viewFormController.keyValue = keyValue;
			viewFormController.initialValues = initialValues;
			viewFormController.formLayout = returnValue;
			[jump2NaviController pushViewController:viewFormController animated:YES];
			[viewFormController getDataFromServer];
			
			[viewFormController release];
			
		}
		else {
			ViewFormController *viewFormController = [[ViewFormController alloc]initWithStyle:UITableViewStylePlain];
			viewFormController.dataObjectName = dataObjectName;
			viewFormController.layoutName = layoutName;
			viewFormController.keyValue = keyValue;
			viewFormController.initialValues = initialValues;
			viewFormController.formLayout = returnValue;
			[jump2NaviController pushViewController:viewFormController animated:YES];
			[viewFormController getDataFromServer];
			
			[viewFormController release];
		}		
	} 
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void) operateDataSuccess:(NSMutableDictionary *)returnValue {
	@try {
		
		NSMutableDictionary *obj = (NSMutableDictionary *)returnValue;
		NSString *message = [obj objectForKey:@"msg"];
		NSMutableDictionary *successUrl = [obj objectForKey:@"url"];
		if (successUrl == nil || (NSNull *)successUrl == [NSNull null] || [[successUrl objectForKey:@"actionType"] isEqualToString:@"empty_url"]) {
			[[MCErrorManagement instance] doError:@"99" returnMessage:message];
			if ([MCFunction isEditForm:jump2NaviController.topViewController]) {
				[MCFunction refreshAndGoParentView:jump2NaviController.topViewController];
			}
			else {
				[MCFunction refreshCurrentView:jump2NaviController.topViewController];
			}	
			return;
		}
		[MCFunction replaceAndGo:successUrl parameters:parameters confirmMessage:nil naviController:jump2NaviController];
	}
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void) doServiceByRecordSuccess: (NSMutableDictionary *)returnValue {
	@try {
		
		bool unVisiable = NO;
		if ([url objectForKey:@"uv"]) {
			unVisiable = [[url objectForKey:@"uv"]boolValue];
		}
		NSMutableDictionary *obj = (NSMutableDictionary *)returnValue;
		NSString *message = [obj objectForKey:@"msg"];
		NSMutableDictionary *successUrl = [obj objectForKey:@"url"];
		if (unVisiable) {
			if (successUrl == nil || (NSNull *)successUrl == [NSNull null] || [[successUrl objectForKey:@"actionType"] isEqualToString:@"empty_url"]) {
				if ([MCFunction isEditForm:jump2NaviController.topViewController]) {
					[MCFunction refreshAndGoParentView:jump2NaviController.topViewController];
				}
				else {
					[MCFunction refreshCurrentView:jump2NaviController.topViewController];
				}	
				return;
			}
			[MCFunction replaceAndGo:successUrl parameters:parameters confirmMessage:nil naviController:jump2NaviController];
			return;
		}
		
		if (successUrl == nil || (NSNull *)successUrl == [NSNull null] || [[successUrl objectForKey:@"actionType"] isEqualToString:@"empty_url"]) {
			[[MCErrorManagement instance] doError:@"99" returnMessage:message];
			if ([jump2NaviController.viewControllers count] == 1 && jump2NaviController.tabBarController) {
				jump2NaviController.tabBarController.selectedIndex = 0;
			}
			
			if ([MCFunction isEditForm:jump2NaviController.topViewController]) {
				[MCFunction refreshAndGoParentView:jump2NaviController.topViewController];
			}
			else {
				[MCFunction refreshCurrentView:jump2NaviController.topViewController];
			}	
			return;
		}
		[MCFunction replaceAndGo:successUrl parameters:parameters confirmMessage:nil naviController:jump2NaviController];
	}
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void) calcSuccess: (NSMutableDictionary *)returnValue {
	@try {
		UIViewController *controller = jump2NaviController.topViewController;
		if ([controller isMemberOfClass:[PopUpSelectFieldViewController class]]) {
			controller = [jump2NaviController.viewControllers objectAtIndex:([jump2NaviController.viewControllers count] - 2)];
		}
		if ([controller isMemberOfClass:[EditFormController class]]) {
			//if ([returnValue objectForKey:@"name"]&&[returnValue objectForKey:@"value"]) {
            //				NSMutableDictionary *fieldData = [NSMutableDictionary dictionary];
            //				[fieldData setObject:[returnValue objectForKey:@"value"] forKey:@"value"];
            //				[(EditFormController *)controller setFormulaData:[returnValue objectForKey:@"name"] fieldData:fieldData];
            //			}
			[(EditFormController *)controller setFormulaData:returnValue];
		} 
	}
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void) getTreeSuccess: (NSMutableDictionary *)returnValue {
	@try {
		if ([[returnValue objectForKey:@"ns"] count] <= 0) {
			[[MCErrorManagement instance] doError:@"99" returnMessage:@"无相关内容！"];
			return;
		}
		
		if (![MCUserDefaults instance].useShrinkTreeView) {
			TreeViewController *treeviewController = [[TreeViewController alloc] initWithStyle:UITableViewStylePlain];
			treeviewController.treeData = returnValue;
			[jump2NaviController pushViewController:treeviewController animated:YES];
			[treeviewController release];
		} else {
			ShrinkTreeViewController *shrinkTreeviewController = [[ShrinkTreeViewController alloc] init];
			shrinkTreeviewController.treeData = returnValue;
			[jump2NaviController pushViewController:shrinkTreeviewController animated:YES];
			[shrinkTreeviewController release];
		}
	} 
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void) getReportConfigSuccess: (NSMutableDictionary *)returnValue {
	@try {
		
		NSString *reportDefId = [url objectForKey:@"rptid"];
		NSMutableArray *queryFilter = [NSMutableArray array];
		if ([url objectForKey:@"qflt"]) {
			queryFilter = [url objectForKey:@"qflt"];
		} else {
			queryFilter = [NSMutableArray array];
		}
        
		NSMutableDictionary *reportQueryLayout = returnValue;
		
		if (reportQueryLayout == nil || [reportQueryLayout count] == 0) {
			ReportViewController *reportViewController = [[ReportViewController alloc]init];
			reportViewController.reportDefId = reportDefId;
			reportViewController.initialValues = queryFilter;
			[jump2NaviController pushViewController:reportViewController animated:YES];
			[reportViewController getDataFromServer];
			[reportViewController release];
		}
		else {
			QueryController *queryController = [[QueryController alloc] initWithStyle:UITableViewStylePlain];
			queryController.reportDefId = reportDefId;
			queryController.listLayout = reportQueryLayout;
			queryController.queryFilter = queryFilter;
			[jump2NaviController pushViewController:queryController animated:YES];
			[queryController release];	
		}
	} 
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void) getCustomFormSuccess: (NSMutableDictionary *)returnValue {
	@try {
		
		NSString *layoutName = [url objectForKey:@"id"];
		NSMutableArray *initialValues = [url objectForKey:@"qflt"] == nil?[NSMutableArray array]:[url objectForKey:@"qflt"];
		NSString *key = [url objectForKey:@"pkey"] == nil?@"":[url objectForKey:@"pkey"];
		
		CustomFormController *customFormController = [[[[MCUserDefaults instance] class:@"CustomFormController"] alloc] initWithStyle:UITableViewStylePlain];
		customFormController.customId = layoutName;
		customFormController.keyValue = key;
		customFormController.initialValues = initialValues;
		customFormController.formLayout = returnValue;
		[jump2NaviController pushViewController:customFormController animated:YES];
		[customFormController release];
	} 
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void) getAppsSuccess: (NSMutableDictionary *)returnValue {
	@try {
		
		MenuController *menuViewController = [[MenuController alloc] init];
		menuViewController.menus = [MenuController removeSeqMenu:(NSMutableArray *)returnValue];
		menuViewController.title = [MCFunction getAppName];
		menuViewController.isApp = YES;
		[jump2NaviController pushViewController:menuViewController animated:YES];
		[menuViewController release];
	} 
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void) getMenuOfAppSuccess: (NSMutableDictionary *)returnValue {
	@try {
		
		MenuController *menuViewController = [[MenuController alloc] init];
		menuViewController.menus = [MenuController removeSeqMenu:(NSMutableArray *)returnValue];
		menuViewController.isApp = NO;
		menuViewController.title = menuTitle == nil?@"":menuTitle;
		[jump2NaviController pushViewController:menuViewController animated:YES];
		[menuViewController release];
	} 
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void) getSubMenuSuccess: (NSMutableDictionary *)returnValue {
	@try {
		
		MenuController *menuViewController = [[MenuController alloc] init];
		menuViewController.menus = [MenuController removeSeqMenu:(NSMutableArray *)returnValue];
		menuViewController.isApp = NO;
		menuViewController.title = menuTitle == nil?@"":menuTitle;
		[jump2NaviController pushViewController:menuViewController animated:YES];
		[menuViewController release];
	} 
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];
	}
}

- (void)getDataFromServerSuccess: (NSMutableDictionary *)returnValue//处理配置数据表返回数据及一些公共的数据表返回数据。huotianjun 不同界面的数据表返回数据由不同的界面处理（分两阶段，第一阶段取到配置表后，创建界面对象，创建完成后，取界面数据，取到后，调用界面里的getDataFromServerSuccess处理逻辑，其中调用loadData更新界面内容）。
{
	switch (actionTypeId) {
		case ActionTypeId_Forward_list:
			[self getListConfigSuccess:returnValue];
			break;
		case ActionTypeId_Forward_Edit:
			[self getEditFormConfigSuccess:returnValue];
			break;
		case ActionTypeId_Forward_View:
			[self getViewFormConfigSuccess:returnValue];
			break;
		case ActionTypeId_Save:
			[self operateDataSuccess:returnValue];
			break;
		case ActionTypeId_Delete:
			[self operateDataSuccess:returnValue];
			break;
		case ActionTypeId_Biz_Service:
			[self doServiceByRecordSuccess:returnValue];
			break;
		case ActionTypeId_Calc:
			[self calcSuccess:returnValue];
			break;
		case ActionTypeId_Forward_Tree:
			[self getTreeSuccess:returnValue];
			break;
		case ActionTypeId_Report_Query:
			[self getReportConfigSuccess:returnValue];
			break;
		case ActionTypeId_Forward_Custom_Form:
			[self getCustomFormSuccess:returnValue];
			break;
		case ActionTypeId_Apps:
			[self getAppsSuccess:returnValue];
			break;
		case ActionTypeId_MenuOfApp:
			[self getMenuOfAppSuccess:returnValue];
			break;
		case ActionTypeId_SubMenu:
			[self getSubMenuSuccess:returnValue];
			break;
		default:
			break;
	}
}
- (void)getServerError: (NSString *)returnCode returnMessage:(NSString *)returnMessage {
	[[MCErrorManagement instance] doError:returnCode returnMessage:returnMessage];
}

@end
